package com.icoopoo.common.base.icoopoo.core.context;

import com.icoopoo.common.base.icoopoo.util.IcoopooPropertiesUtil;
import com.icoopoo.common.base.icoopoo.util.StringUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 *
 */
public final class PropertyPlaceholder extends PropertyPlaceholderConfigurer {
    private List<String> icooProperties;

    public PropertyPlaceholder(){
    }

    @Override
    protected void loadProperties(Properties props) throws IOException {
        super.loadProperties(props);
        String keyStr;
        String value;
        for (Iterator keys = props.keySet().iterator(); keys.hasNext(); IcoopooPropertiesUtil.getProperties().put(keyStr, value)){
            Object key = keys.next();
            keyStr = key.toString();
            value = props.getProperty(keyStr);
            //加密数据库
            if(this.icooProperties != null && this.icooProperties.contains(keyStr)) {
                String dkey = props.getProperty("druid.key");
                dkey = StringUtil.isEmpty(dkey)?"10086111":dkey;
                value = SecurityUtil.decryptDes(value, dkey.getBytes());
                props.setProperty(keyStr, value);
            }
        }
    }

    public void setDecryptProperties(List<String> icooProperties) {
        this.icooProperties = icooProperties;
    }

    public static void main(String[] args) {
        String encrypt = SecurityUtil.encryptDes("theloveIcoopoo", "10086111".getBytes());
        System.out.println(encrypt);
        System.out.println(SecurityUtil.decryptDes(encrypt, "10086111".getBytes()));
    }
}
